# Guile-GCC
# Copyright (C) 2012 Ludovic Courtès <ludo@gnu.org>
#
# Guile-GCC is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Guile-GCC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Guile-GCC.  If not, see <http://www.gnu.org/licenses/>.

# `LIBRARIES' would be more appropriate than `LTLIBRARIES' but it
# requires a name prefixed by `lib'.
gccplugin_LTLIBRARIES = guile.la

BUILT_SOURCES = src/guile.x src/cgraph.x src/gimple.x

CLEANFILES =					\
  $(BUILT_SOURCES) call-graph.dot		\
  examples/*.c.[0-9]*.gimple

dist_noinst_HEADERS = src/ggcc-utils.h
guile_la_SOURCES = src/guile.c src/cgraph.c src/gimple.c
guile_la_LDFLAGS = -module
guile_la_LIBADD = $(GUILE_LIBS)
guile_la_CFLAGS =					\
  -I$(builddir)/src -I$(srcdir)/src			\
  -I$(GCC_PLUGIN_INCLUDE_DIR) $(GUILE_CFLAGS) -Wall

.c.x:
	$(GUILE_SNARF) -o "$@" "$<" $(guile_la_CFLAGS)

# `nobase_' cannot be used because of the leading `src/', so handle
# that manually.
gccguilemoduledir = $(guilemoduledir)/gcc

dist_guilemodule_DATA = src/gcc.scm
dist_gccguilemodule_DATA =			\
  src/gcc/cpp.scm				\
  src/gcc/cgraph.scm				\
  src/gcc/gimple.scm

#
# Programs.
#

ggcc_cflags =							\
  -fplugin="$(top_builddir)/.libs/guile.so"			\
  -fplugin-arg-guile-module-directory="$(top_srcdir)/src"

nobase_noinst_PROGRAMS =			\
  examples/birthday				\
  examples/meta					\
  examples/call-graph

dist_noinst_DATA =				\
  examples/birthday.scm				\
  examples/meta.scm				\
  examples/call-graph.scm


TESTS_ENVIRONMENT =						\
  GUILE_LOAD_PATH="$(abs_top_srcdir)/tests:$GUILE_LOAD_PATH"	\
  ./tests/run-test

TESTS =						\
  tests/pragma.c				\
  tests/cgraph.c

TEST_PLUGINS =					\
  tests/pragma.scm				\
  tests/cgraph.scm

CLEANFILES +=					\
  *.gimple					\
  tests/pragma					\
  tests/cgraph.o

EXTRA_DIST = $(TESTS) $(TEST_PLUGINS)


# Files the example programs depend on.
example_deps = guile.la $(nobase_dist_guilemodule_DATA)

$(examples_birthday_OBJECTS): examples/birthday.scm $(example_deps)
examples_birthday_CFLAGS =					\
  $(ggcc_cflags)						\
  -fplugin-arg-guile-load=$(srcdir)/examples/birthday.scm

$(examples_meta_OBJECTS): examples/meta.scm $(example_deps)
examples_meta_CFLAGS =					\
  $(ggcc_cflags)					\
  -fdump-tree-gimple					\
  -fplugin-arg-guile-load=$(srcdir)/examples/meta.scm

$(examples_call_graph_OBJECTS): examples/call-graph.scm $(example_deps)
examples_call_graph_CFLAGS =					\
  $(ggcc_cflags)						\
  -fdump-tree-gimple						\
  -fplugin-arg-guile-load=$(srcdir)/examples/call-graph.scm	\
  -fplugin-arg-guile-dot-file="$(builddir)/call-graph.dot"

ACLOCAL_AMFLAGS = -I m4

.PHONY: gen-ChangeLog
gen-ChangeLog:
	if test -d .git; then				\
	  $(top_srcdir)/build-aux/gitlog-to-changelog	\
	    > $(distdir)/cl-t;				\
	  rm -f $(distdir)/ChangeLog;			\
	  mv $(distdir)/cl-t $(distdir)/ChangeLog;	\
	fi

dist-hook: gen-ChangeLog
